package cn.edu.hitwh.yibao.service.impl;

import java.util.List;

import cn.edu.hitwh.yibao.entity.Facility;
import cn.edu.hitwh.yibao.form.FacilityForm;
import cn.edu.hitwh.yibao.mapper.FacilityMapper;
import cn.edu.hitwh.yibao.service.FacilityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

@Service
public class FacilityServiceImpl implements FacilityService {
	@Autowired
	private FacilityMapper facilityMapper;

	@Override
	public String select(FacilityForm facilityForm, Model model) {
		Facility fac = new Facility();
		BeanUtils.copyProperties(facilityForm, fac);
		List<Facility> list = facilityMapper.selectList(fac);
		model.addAttribute("facList", list);
		return "/facility/select";
	}

	@Override
	public String add(FacilityForm facilityForm, Model model) {
		Facility fac = new Facility();
		BeanUtils.copyProperties(facilityForm, fac);
		int count = facilityMapper.insert(fac);
		if (count > 0) {
			return "redirect:/facility/addComplete";
		} else {
			model.addAttribute("addResult","添加设施失败！");
			return "/facility/select";
		}
	}

	@Override
	public String updateInit(String serId, Model model) {
		Facility fac = facilityMapper.selectById(serId);
		model.addAttribute("fac", fac);
		return "/facility/update";
	}

	@Override
	public String update(FacilityForm facilityForm, Model model) {
		Facility fac = new Facility();
		BeanUtils.copyProperties(facilityForm,fac);
		int count = facilityMapper.update(fac);
		if (count > 0) {
			return "redirect:/facility/updateComplete";
		} else {
			return "/facility/select";
		}
	}

	@Override
	public String delete(String serId, Model model) {
		int count = facilityMapper.delete(serId);
		if (count > 0) {
			return "redirect:/facility/deleteComplete";
		} else {
			model.addAttribute("deleteResult","删除药品失败！");
			return "/facility/select";
		}
	}

	@Override
	public String detail(String serId, Model model) {
		Facility fac = facilityMapper.selectById(serId);
		model.addAttribute("fac",fac);
		return "/facility/detail";
	}
	
}
